﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class uc_NguonCungCap : DevExpress.XtraEditors.XtraUserControl
    {
        #region Khai báo biến
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        NguonCungCap_BLL ncc_bll = new NguonCungCap_BLL();
        #endregion

        #region LoadForm

        public uc_NguonCungCap()
        {
            InitializeComponent();
        }
        
        private void uc_NguonCungCap_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region Các chức năng
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmNguonCungCap frm = new frmNguonCungCap();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frmNguonCungCap frm = new frmNguonCungCap();
                    frm.maNguonCung = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaNguonCung).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadData();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                NguonCungCap_Public ncc = new NguonCungCap_Public();
                try
                {
                    string ma = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaNguonCung).ToString();
                    ncc.MaNguonCung = ma;
                    string ten = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colTenNguonCung).ToString();
                    dt = ncc_bll.TaoBang(box.sqlCheckExist("DM_TaiLieu", "MaNguonCung", ma));
                    if (dt.Rows.Count > 0)
                    {
                        XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            try
                            {
                                ncc_bll.Delete(ncc);
                                LoadData();
                            }
                            catch (Exception)
                            {
                                XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);

        }
        #endregion

        #region hàm viết thêm

        private void LoadData()
        {
            grd_Data.DataSource = ncc_bll.LayDuLieu();
        }
        #endregion

    }
}
